package com.example.smartgreenhouse.WebSocketServer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class FanStatusWebSocketServer {
    private final SimpMessagingTemplate messagingTemplate;

    @Autowired
    public FanStatusWebSocketServer(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void sendFanStatus(Long fanId, Integer level, Boolean isAuto) {
        Map<String, Object> message = new HashMap<>();
        message.put("fanId", fanId);
        message.put("level", level);
        message.put("isAuto", isAuto);
        message.put("updateTime", System.currentTimeMillis());
        log.info("正在推送WebSocket消息 => 路径: /topic/fan_status, 内容: {}", message);

        messagingTemplate.convertAndSend("/topic/fan_status", message);
    }
}